package com.otod.pm.prj.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonFailure;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.service.TeamService;
import com.otod.pm.prj.vo.TeamVo;
import com.otod.sys.emp.service.IEmpService;
import com.otod.sys.vo.AuapTEmpVo;

/**
 * 团队管理
 * @author jqiu
 */
@Controller
@RequestMapping("pm/team")
public class TeamController extends BaseController {

	@Autowired
	private TeamService teamService;
	
	@Autowired
	private IEmpService empService;
	
	@Autowired
	private ProjectService projectService;


	/**
	 * 保存项目人员信息
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="saveTeam",method=RequestMethod.POST)
	@ResponseBody
	public String saveTeam(String prjId,String roleNo1,String roleNo2) {
		if(StringUtils.isNotEmpty(prjId)){
			teamService.deleteByprjId(prjId);
		}
		TeamVo vo = new TeamVo();
		vo.setPrjId(prjId);
		
		String roleNo = roleNo1.split(";")[0];
		String empNo = roleNo1.split(";")[1];
		vo.setEmpNo(empNo);
		vo.setRoleNo(roleNo);
		teamService.addTeam(vo);
		
		String[] roleNoTeam = roleNo2.split(";");
		for (int i = 1; i < roleNoTeam.length; i++) {
			vo.setRoleNo(roleNoTeam[0]);
			vo.setEmpNo(roleNoTeam[i]);
			teamService.addTeam(vo);
		}
		
		/*if(null != vo){
			if(null != vo.getUuid() && !"".equals(vo.getUuid())){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("uuid", vo.getUuid());
				map.put("prjId", vo.getPrjId());
				map.put("empNo", vo.getEmpNo());
				map.put("roleNo", vo.getRoleNo());
				map.put("startDate", vo.getStartDate());
				map.put("endDate", vo.getEndDate());
				map.put("status", vo.getStatus());
				map.put("remark", vo.getRemark());

				
				teamService.updateTeam(map);
			}else{
				//新增
			
				teamService.addTeam(vo);
			}
		}*/
		return JsonSuccess.factory("保存人员成功");
	}
	
	/**
	 * 查询项目人员信息
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="findByPrjId",method=RequestMethod.POST)
	@ResponseBody
	public List<TeamVo> findByPrjId(String prjId,HttpServletRequest request) {
		Map params = super.getParamMapFromRequest(request);
		List<TeamVo> list = teamService.findByPrjId(prjId);

		return list;
	}
	
	/**
	 * 查询项目人员信息
	 * @param prjId,roleNo
	 * @return
	 */
	@RequestMapping(value="findByPrjIdAndRoleNo",method=RequestMethod.POST)
	@ResponseBody
	public List<TeamVo> findByPrjIdAndRoleNo(HttpServletRequest request) {
		String prjId = request.getParameter("prjId");
		String roleNo = request.getParameter("roleNo");
		List<TeamVo> list = teamService.findByPrjIdAndRoleNo(prjId,roleNo);
		return list;
	}
			
		/**
		 * 查询人员信息
		 * @param vo
		 * @return
		 */
		@RequestMapping(value="findEmpList",method=RequestMethod.POST)
		@ResponseBody
		public List<AuapTEmpVo> findEmpList(@RequestBody String prjId) {
			//prjId = "c21fa5dd7e5b4sf0bb8f8b081b2f0eb0";
			List<AuapTEmpVo> empList = projectService.findEmpNotInTeam(prjId);
			return empList;
		}		
		/**
		 * 保存人员信息
		 * @param vo
		 * @return
		 */
		@RequestMapping(value="saveMember",method=RequestMethod.POST)
		@ResponseBody
		public String saveMember(TeamVo vo) {
			if(vo!=null){
				String empNos = vo.getEmpNo();
				if(Collections3.isNotEmpty(teamService.queryByEmpNoAndPrjId(empNos,vo.getPrjId()))){
					return JsonFailure.factory("人员已添加，请重新选择！");
				}
				//判断该人员角色在该项目中是否已经存在
				String[] empNoList = empNos.split(",");
				for (String empNo : empNoList) {
					vo.setEmpNo(empNo);
					teamService.addTeam(vo);
				}
				return JsonSuccess.factory("保存人员成功");
			}
			return null;
		}		
		/**
		 * 获取当前用户的权限信息
		 * @param prjId
		 * @return
		 */
		@RequestMapping(value="getMemberAuth",method=RequestMethod.POST)
		@ResponseBody
		public String getMemberAuth(String prjId) {
			
			return JsonSuccess.factory(teamService.getAuth(prjId),"true");
		}		
		
		
		/**
		 * 删除项目成员
		 * @param uuid
		 * @return
		 * @throws AppException 
		 */
		@RequestMapping(value ="delete", method = RequestMethod.POST)
		@ResponseBody
		public String list(@RequestBody String uuid) throws AppException{
			if(StringUtils.isEmpty(uuid)){
				throw new AppException("UUID不能为空！");
			}

			String msg = teamService.deleteByUuid(uuid);
			return JsonSuccess.factory(msg);
		}
	
}
